﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using App1AppHandler;
using Reborn_WorldServer;

namespace Reborn_Server.App1AppHandler
{
    public class Spell_WallStun :Buff
    {
        public Spell_WallStun()
        {
            spellTimer.Interval = 1500;
            minutesLeft = -2;
            ticksLeft = 6;
        }

        override public void Resume(CreatureBase targetCreatureBase, SpellIcon _spellIcon, int timeRemaining, int castLevel, int castCharacterID, int castInt, int castWis, int optionalValue, string xmlData)
        {
            ticksLeft = timeRemaining;

            base.Resume(targetCreatureBase, _spellIcon, timeRemaining, castLevel, castCharacterID, castInt, castWis, optionalValue, xmlData);

            

            if (targetCreatureBase is Character)
            {
                Character targetCharacter = (Character)targetCreatureBase;
                targetCharacter.buffManager.UpdateBuffs();
                targetCharacter.SetBusy(true);

                Message msg = new Message();

                msg = new Message();
                msg.Type = "damaged";
                msg.CreatureID = targetCreatureBase.CreatureID;
                msg.Text = "You ran into something!";
                targetCharacter.AddMessage(msg);

                msg = new Message();
                msg.CreatureID = targetCharacter.CreatureID;
                msg.Text = "You are stunned!";
                msg.Type = "damaged";

                targetCharacter.AddMessage(msg);
            }

            spellTimer.Start();
        }

        public void Cast(CreatureBase targetCreatureBase)
        {
            TargetCreatureBase = targetCreatureBase;

            SpellID = Managers.GameHandler.ItemIDGen.GetSpellID();

            foreach (KeyValuePair<int, SpellIcon> spellicon in targetCreatureBase.GameHandler.Spells)
            {
                if (spellicon.Value.Name == "Stun")
                {
                    spellIcon = spellicon.Value;
                    break;
                }
            }

            //Add Buff to creature base
            targetCreatureBase.buffManager.AddBuff(this);

            if (targetCreatureBase is Character)
            {
                Character targetCharacter = (Character)targetCreatureBase;
                targetCharacter.buffManager.UpdateBuffs();
                targetCharacter.SetBusy(true);

                Message msg = new Message();

                msg = new Message();
                msg.Type = "damaged";
                msg.CreatureID = targetCreatureBase.CreatureID;
                msg.Text = "You ran into something!";
                targetCharacter.AddMessage(msg);

                msg = new Message();
                msg.CreatureID = targetCharacter.CreatureID;
                msg.Text = "You are stunned!";
                msg.Type = "damaged";

                targetCharacter.AddMessage(msg);
            }
            base.spellTimer.Start();
        }

        public override void RemoveSpell(bool updateBuffs, bool sendMessage)
        {
            //This is not fullproof... just incase timer expired thread runs at the same time try to avoid double removing
            ticksLeft = -1;

            base.RemoveSpell(updateBuffs, sendMessage);
            if (TargetCreatureBase is Character)
            {
                Character targetCharacter = (Character)TargetCreatureBase;
                targetCharacter.SetBusy(false);
            }
        }

        protected override void SpellTimer_Expired(object source, System.Timers.ElapsedEventArgs e)
        {
            LockItem lockitem = TargetCreatureBase.GameHandler.GetLockItem(TargetCreatureBase.MapID, TargetCreatureBase.Instance);

            lock (lockitem.LockObj)
            {
                //This buff has been removed, do nothing (this can occur if the buff gets overwritten exactly when this timer fires)
                if (TargetCreatureBase.buffManager.hasBuff(this.SpellID) == false)
                    return;

                if (ticksLeft < 0)
                    return;

                if (ticksLeft == 0)
                {
                    RemoveSpell(true, true);
                    return;
                }

                ticksLeft--;

                if (TargetCreatureBase is Character)
                {
                    Character targetCharacter = (Character)TargetCreatureBase;

                    Message msg = new Message();
                    msg.CreatureID = targetCharacter.CreatureID;
                    msg.Text = "You are stunned!";
                    msg.Type = "damaged";

                    targetCharacter.AddMessage(msg);
                }
                base.spellTimer.Start();
            }
        }
    }
}
